package com.example.leetcode.solution;

/**
 * 5. 最长回文子串
 * 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 *
 * 示例 1：
 *
 * 输入: "babad"
 * 输出: "bab"
 * 注意: "aba" 也是一个有效答案。
 * 示例 2：
 *
 * 输入: "cbbd"
 * 输出: "bb"
 */
public class Solution5 {

    public String longestPalindrome(String s) {
        String result = "";
        for (int index = 0; index < s.length(); index++) {
            String result2 = String.valueOf(s.charAt(index));
            int toLeft = index - 1;
            int toRight = index + 1;
            String temp = result2;
            while (toLeft >= 0 && toRight < s.length()) {
                String leftTemp = String.valueOf(s.charAt(toLeft));
                String rightTemp = String.valueOf(s.charAt(toRight));
                if (leftTemp.equals(rightTemp)) {
                    result2 = leftTemp + temp + rightTemp;
                } else {
                    boolean allMatch1 = true;
                    for (int i = 0; i < result2.length(); i++) {
                        if (!leftTemp.equals(String.valueOf(result2.charAt(i)))) {
                            allMatch1 = false;
                            break;
                        }
                    }
                    if (allMatch1) {
                        result2 += leftTemp;
                        temp = result2;
                    } else {
                        toLeft++;
                    }

                    boolean allMatch = true;
                    for (int i = 0; i < result2.length(); i++) {
                        if (!rightTemp.equals(String.valueOf(result2.charAt(i)))) {
                            allMatch = false;
                            break;
                        }
                    }
                    if (allMatch) {
                        result2 += rightTemp;
                        temp = result2;
                    } else {
                        toRight--;
                    }
                    if (!allMatch && !allMatch1) {
                        toLeft = -1;
                    }
                }
                toLeft--;
                toRight++;
                temp = result2;

            }
            if (toLeft >= 0) {

                String leftTemp = String.valueOf(s.charAt(toLeft));
                boolean allMatch1 = true;
                for (int i = 0; i < result2.length(); i++) {
                    if (!leftTemp.equals(String.valueOf(result2.charAt(i)))) {
                        allMatch1 = false;
                        break;
                    }
                }
                if (allMatch1) {
                    result2 += leftTemp;
                    temp = result2;
                }
            }
            if (toRight < s.length()) {
                String rightTemp = String.valueOf(s.charAt(toRight));
                boolean allMatch = true;
                for (int i = 0; i < result2.length(); i++) {
                    if (!rightTemp.equals(String.valueOf(result2.charAt(i)))) {
                        allMatch = false;
                        break;
                    }
                }
                if (allMatch) {
                    result2 += rightTemp;
                    temp = result2;
                }
            }
            if (result2.length() > result.length()) {
                result = result2;
            }
        }
        return result;
    }
}